package danger.action.sys.dic;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;

import danger.bean.sys.Dictionary;
import danger.service.sys.DictionaryService;
import danger.utils.PageConstants;
import net.sf.json.JSONArray;

/**
 * 查询字典相关信息
 *
 */
@Component
public class SearchDictionaryAction extends ActionSupport {

	private static final long serialVersionUID = -2718753766487238403L;
	
	private Dictionary dictionary;
	private String upDictionaryId; //上级字典编号  方法一和方法二公用
	private String result;               //返回的结果
	private String nextId;               //下级中下一个字典的编号 
	private String dictionaryId;   //字典的编号  查询条件
	private String name; //字典的名字 查询条件
	private String currentPage; 	     //当前页码
	private int totalPage;               //总页数
	
	@Resource
	private DictionaryService dictionaryService;
	
	/**
	 * 按照上级字典编号查询字典信息
	 * @return  
	 */
	public String searchDictionaryByUpId(){
		List<Dictionary> dictionary;
		try {
			Object pageSize = new PageConstants().getPageSize();
			Object pageBegin = new PageConstants().getRecordNums(Integer.parseInt(currentPage));
			Map<String,Object> dicCondition = new HashMap<String,Object>();
			dicCondition.put("upDictionaryId",upDictionaryId);
			dicCondition.put("pageBegin", pageBegin);
			dicCondition.put("pageSize", pageSize);
			//返回自己的子节点
			dictionary = dictionaryService.getDictionaryByConditon(dicCondition);
			//将java对象转化为json对象
			JSONArray jsonArray = JSONArray.fromObject(dictionary);
			//返回给Ajax
			this.result = jsonArray.toString();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	/**
	 * 按条件查询字典信息
	 * @return
	 */
	public String searchDictionaryByCondition(){
		List<Dictionary> dictionary;
		try {
			Object pageSize = new PageConstants().getPageSize();
			Object pageBegin = new PageConstants().getRecordNums(Integer.parseInt(currentPage));
			Map<String,Object> condition = new HashMap<String,Object>();
			if(dictionaryId!=null && ("").equals(dictionaryId)){
				dictionaryId = null;
			}
			if(name!=null && ("").equals(name)){
				name = null;
			}
			if(upDictionaryId!=null && ("").equals(upDictionaryId)){
				upDictionaryId = null;
			}
			condition.put("dictionaryId", dictionaryId);     //字典编号
			condition.put("name", name); //字典名称
			condition.put("pageBegin", pageBegin);
			condition.put("pageSize", pageSize);
			condition.put("upDictionaryId", upDictionaryId);
			dictionary = dictionaryService.getDictionaryByConditon(condition);
			//将java对象转化为json对象
			JSONArray jsonArray = JSONArray.fromObject(dictionary);
			//返回给Ajax
			this.result = jsonArray.toString();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String searchDicCountByCondition(){
		try {
			Map<String,Object> condition = new HashMap<String,Object>();
			if(dictionaryId!=null && ("").equals(dictionaryId)){
				dictionaryId = null;
			}
			if(name!=null && ("").equals(name)){
				name = null;
			}
			if(upDictionaryId!=null && ("").equals(upDictionaryId)){
				upDictionaryId = null;
			}
			condition.put("dictionaryId", dictionaryId);     //字典编号
			condition.put("name", name); //字典名称
			condition.put("upDictionaryId", upDictionaryId); //上级字典编号
			int totalCount = dictionaryService.getDicCountByConditon(condition);
			this.totalPage = new PageConstants().getPages(totalCount);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	/**
	 * 查询即将新增的字典的编号
	 * @return  
	 */
	public String searchNextId(){
		try {
			this.nextId = (dictionaryService.getNextDictionaryId(upDictionaryId));
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	
	public String getDictionaryId() {
		return dictionaryId;
	}

	public void setDictionaryId(String dictionaryId) {
		this.dictionaryId = dictionaryId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Dictionary getDictionary() {
		return dictionary;
	}

	public void setDictionary(Dictionary dictionary) {
		this.dictionary = dictionary;
	}

	public String getUpDictionaryId() {
		return upDictionaryId;
	}

	public void setUpDictionaryId(String upDictionaryId) {
		this.upDictionaryId = upDictionaryId;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}
	public String getNextId() {
		return nextId;
	}

	public void setNextId(String nextId) {
		this.nextId = nextId;
	}

	public String getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(String currentPage) {
		this.currentPage = currentPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
}
